package com.font.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.font.bean.Face;
import com.font.bean.ParamResult;
import com.font.bean.SysUser;
import com.font.bean.result.Result;
import com.font.execiption.GuiguException;
import com.font.mapper.SysUserMapper;
import com.font.redis.RedisCache;
import com.font.service.FaceService;
import com.font.service.LoginService;
import com.font.utils.JwtUtils;
import com.font.utils.MD5;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import java.util.HashMap;
import java.util.Map;

@Service
public class LoginServiceImpl implements LoginService {

    @Autowired
    SysUserMapper sysUserMapper;
    @Autowired
    FaceService faceService;
    @Autowired
    RedisCache redisCache;
    @Override
    public Result sent(String email) {
        return  new Result(200,"====完成发送！====");
    }

    @Override
    public Result login(ParamResult parm) {
        System.err.println(parm);
        if (StringUtils.hasText(parm.getEmailCode())){
            Integer emailCode = redisCache.getCacheObject("emailCode");
            System.err.println(emailCode== Integer.parseInt(parm.getEmailCode()));
            if (emailCode== Integer.parseInt(parm.getEmailCode()) ){
                SysUser sysUser = sysUserMapper.selectOne(new LambdaQueryWrapper<SysUser>().eq(SysUser::getEmail, parm.getEmail()));
                Map<String,String> map = new HashMap<>();
                Result result = new Result();
                map.put("faceName", sysUser.getUserName());
                result.setToken(JwtUtils.genereteToken(map));
                result.setName(sysUser.getNickName());
                result.setData(sysUser);
                result.setMsg("登录成功");
                result.setCode(Result.SUCCESS_CODE);
                return result;
            }
            throw new GuiguException(201,"邮箱验证码不正确");
        }else {
            String code=redisCache.getCacheObject("code");
            System.err.println(code.equals(parm.getCode()));
            System.err.println(code);
            System.err.println(parm.getCode());
            if (code.equals(parm.getCode())){
                Face one = faceService.getOne(new LambdaQueryWrapper<Face>().eq(Face::getFaceName, parm.getUserName()));
                String passWord = MD5.encrypt(parm.getPassWord());
                if (one.getPassWord().equals(passWord)){
                    Map<String,String> map = new HashMap<>();
                    Result result = new Result();
                    map.put("faceName", one.getFaceName());
                    result.setToken(JwtUtils.genereteToken(map));
                    result.setName( one.getFaceName());
                    result.setMsg("登录成功");
                    result.setCode(Result.SUCCESS_CODE);
                    result.setData(sysUserMapper.selectOne(new LambdaQueryWrapper<SysUser>().eq(SysUser::getUserName,one.getFaceName())));
                    return result;
                }
                throw new GuiguException(201,"账户或者密码不正确");
            }
            throw new GuiguException(201,"验证码不正确");
        }

    }

    @Override
    public Result getToken(ParamResult parm) {
        Face one = faceService.getOne(new LambdaQueryWrapper<Face>().eq(Face::getFaceName, parm.getUserName()));
        Map<String,String> map = new HashMap<>();
        Result result = new Result();
        map.put("faceName", one.getFaceName());
        result.setToken(JwtUtils.genereteToken(map));
        result.setName( one.getFaceName());
        result.setMsg("登录成功");
        result.setCode(Result.SUCCESS_CODE);
        result.setData(sysUserMapper.selectOne(new LambdaQueryWrapper<SysUser>().eq(SysUser::getUserName,one.getFaceName())));
        return result;
    }

    @Override
    @Transactional
    public Result register(ParamResult parm) {
        String code = redisCache.getCacheObject("code");
        String passWord = MD5.encrypt(parm.getPassWord());
        if (parm.getCode().equals(code)){
            SysUser sysUser = new SysUser();sysUser.setUserName(parm.getUserName());sysUser.setPassword(passWord);sysUser.setStatus("1");sysUser.setDelFlag("1");sysUser.setNickName(parm.getUserName());
            sysUserMapper.insert(sysUser);
            Face face = new Face();
            face.setFaceName(parm.getUserName());face.setPassWord(passWord);face.setFaceStatus(1);
            faceService.save(face);
            Result result = new Result();
            Map<String,String> map = new HashMap<>();
            map.put("faceName", parm.getUserName());
            result.setToken(JwtUtils.genereteToken(map));
            result.setName( parm.getUserName());
            result.setMsg("登录成功");
            result.setCode(Result.SUCCESS_CODE);
            result.setData(sysUser);
            return result;
        }else {
            return new Result(201,"验证码错误");
        }




    }

    @Override
    public Face checkUsername(String userName) {
        Face one = faceService.getOne(new LambdaQueryWrapper<Face>().eq(Face::getFaceName, userName));
        return one;
    }


}
